//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "CTSMMCapletOriginalCalibration.h"
using namespace Cephei::QL::Models::Marketmodels::Models;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/PiecewiseConstantCorrelation.h>
#include <gen/QL/Models/Marketmodels/Models/PiecewiseConstantVariance.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Math/Matrix.h>
#include <gen/QL/Models/Marketmodels/Models/CTSMMCapletCalibration.h>
using namespace Cephei::QL::Models::Marketmodels;
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::CCTSMMCapletOriginalCalibration (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, Cephei::QL::Models::Marketmodels::IPiecewiseConstantCorrelation^ corr, Cephei::Core::IVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ displacedSwapVariances, Cephei::Core::IVector<Double>^ capletVols, Cephei::QL::Models::Marketmodels::ICurveState^ cs, Double displacement, Cephei::Core::IVector<Double>^ alpha, Boolean lowestRoot, Boolean useFullApprox) : CCTSMMCapletCalibration(CCTSMMCapletOriginalCalibration::typeid)
{
    CEvolutionDescription^ _Cevolution;
    CPiecewiseConstantCorrelation^ _Ccorr;
    CCurveState^ _Ccs;
    try
    {
#ifdef HANDLE
        _phCTSMMCapletOriginalCalibration = NULL;
#endif
        _Cevolution = safe_cast<CEvolutionDescription^> (evolution);
        _Cevolution->Lock();
        QuantLib::EvolutionDescription& _evolution = static_cast<QuantLib::EvolutionDescription&> (_Cevolution->GetReference ()); 
        _Ccorr = safe_cast<CPiecewiseConstantCorrelation^> (corr);
        _Ccorr->Lock();
        boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation>& _corr = static_cast<boost::shared_ptr<QuantLib::PiecewiseConstantCorrelation>&> (_Ccorr->GetShared ()); 
        displacedSwapVariances ->Lock ();
        INativeVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ _NCIdisplacedSwapVariances = displacedSwapVariances->getFeature (NativeFeature::shared_ptr);
        CPiecewiseConstantVarianceVector^ _NCdisplacedSwapVariances = safe_cast<CPiecewiseConstantVarianceVector^>(_NCIdisplacedSwapVariances);
        std::vector<boost::shared_ptr<
                        QuantLib::PiecewiseConstantVariance> >& _displacedSwapVariances = static_cast<std::vector<boost::shared_ptr<
                        QuantLib::PiecewiseConstantVariance> >&> (_NCdisplacedSwapVariances->GetShared ());
        capletVols->Lock();
        INativeVector<Double>^ _NCIcapletVols = capletVols->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcapletVols = safe_cast<CDoubleVector^>(_NCIcapletVols);
        std::vector<QuantLib::Volatility>& _capletVols = static_cast<std::vector<QuantLib::Volatility>&> (_NCcapletVols->GetReference ());
        _Ccs = safe_cast<CCurveState^> (cs);
        _Ccs->Lock();
        boost::shared_ptr<QuantLib::CurveState>& _cs = static_cast<boost::shared_ptr<QuantLib::CurveState>&> (_Ccs->GetShared ()); 
        QuantLib::Spread _displacement = (QuantLib::Spread)ValueHelper::Convert (displacement); //d
        alpha->Lock();
        INativeVector<Double>^ _NCIalpha = alpha->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCalpha = safe_cast<CDoubleVector^>(_NCIalpha);
        std::vector<QuantLib::Real>& _alpha = static_cast<std::vector<QuantLib::Real>&> (_NCalpha->GetReference ());
        bool _lowestRoot = (bool)ValueHelper::Convert (lowestRoot); //d
        bool _useFullApprox = (bool)ValueHelper::Convert (useFullApprox); //d
        _ppCTSMMCapletOriginalCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletOriginalCalibration> (new QuantLib::CTSMMCapletOriginalCalibration ( _evolution,  _corr,  _displacedSwapVariances,  _capletVols,  _cs,  _displacement,  _alpha,  _lowestRoot,  _useFullApprox ));
        SetCTSMMCapletCalibration (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletOriginalCalibration));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cevolution != nullptr) _Cevolution->Unlock();
        if (_Ccorr != nullptr) _Ccorr->Unlock();
        if (displacedSwapVariances != nullptr) displacedSwapVariances->Unlock();    //not optional
        if (capletVols != nullptr) capletVols->Unlock();    //not optional
        if (_Ccs != nullptr) _Ccs->Unlock();
        if (alpha != nullptr) alpha->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::CCTSMMCapletOriginalCalibration (boost::shared_ptr<QuantLib::CTSMMCapletOriginalCalibration>& childNative, Object^ owner) : CCTSMMCapletCalibration(CCTSMMCapletOriginalCalibration::typeid)
{
#ifdef HANDLE
	_phCTSMMCapletOriginalCalibration = NULL;
#endif
	_ppCTSMMCapletOriginalCalibration = &childNative;
    _ppCTSMMCapletCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletCalibration> (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletOriginalCalibration));
}
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::CCTSMMCapletOriginalCalibration (QuantLib::CTSMMCapletOriginalCalibration& childNative, Object^ owner) : CCTSMMCapletCalibration(CCTSMMCapletOriginalCalibration::typeid)
{
#ifdef HANDLE
	_phCTSMMCapletOriginalCalibration = NULL;
#endif
	_ppCTSMMCapletOriginalCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletOriginalCalibration> (&childNative);
    _ppCTSMMCapletCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletCalibration> (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletOriginalCalibration));
    _CTSMMCapletOriginalCalibrationOwner = owner;
    _CTSMMCapletCalibrationOwner = owner;
}

Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::CCTSMMCapletOriginalCalibration (CCTSMMCapletOriginalCalibration^ copy) : CCTSMMCapletCalibration(CCTSMMCapletOriginalCalibration::typeid)
{
#ifdef HANDLE
	_phCTSMMCapletOriginalCalibration = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCTSMMCapletOriginalCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletOriginalCalibration> (copy->GetShared());
        _ppCTSMMCapletCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletCalibration> (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletOriginalCalibration));
    }
}
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::CCTSMMCapletOriginalCalibration (PLATFORM::Type^ t) : CCTSMMCapletCalibration(CCTSMMCapletOriginalCalibration::typeid)
{
#ifdef HANDLE
	_phCTSMMCapletOriginalCalibration = NULL;
#endif
	if (!t->IsSubclassOf(CCTSMMCapletOriginalCalibration::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::CCTSMMCapletOriginalCalibration (QuantLib::Handle<QuantLib::CTSMMCapletOriginalCalibration>& childNative, Object^ owner)  : CCTSMMCapletCalibration(CCTSMMCapletOriginalCalibration::typeid)
{
	_phCTSMMCapletOriginalCalibration = &childNative;
	_ppCTSMMCapletOriginalCalibration = &static_cast<boost::shared_ptr<QuantLib::CTSMMCapletOriginalCalibration>>(childNative.currentLink());
    _ppCTSMMCapletCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletCalibration> (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletOriginalCalibration));
    _CTSMMCapletOriginalCalibrationOwner = owner;
}
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::CCTSMMCapletOriginalCalibration (QuantLib::Handle<QuantLib::CTSMMCapletOriginalCalibration> childNative)  : CCTSMMCapletCalibration(CCTSMMCapletOriginalCalibration::typeid)
{
	_phCTSMMCapletOriginalCalibration = &childNative;
	_ppCTSMMCapletOriginalCalibration = &static_cast<boost::shared_ptr<QuantLib::CTSMMCapletOriginalCalibration>>(childNative.currentLink());
    _ppCTSMMCapletCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletCalibration> (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletOriginalCalibration));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::CCTSMMCapletOriginalCalibration (QuantLib::CTSMMCapletOriginalCalibration childNative)  : CCTSMMCapletCalibration(CCTSMMCapletOriginalCalibration::typeid)
{
#ifdef HANDLE
	_phCTSMMCapletOriginalCalibration = NULL;
#endif
	_ppCTSMMCapletOriginalCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletOriginalCalibration> (new QuantLib::CTSMMCapletOriginalCalibration (childNative));
    _ppCTSMMCapletCalibration = new boost::shared_ptr<QuantLib::CTSMMCapletCalibration> (boost::dynamic_pointer_cast<QuantLib::CTSMMCapletCalibration> (*_ppCTSMMCapletOriginalCalibration));
}
#endif

Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::~CCTSMMCapletOriginalCalibration ()
{
    if (_ppCTSMMCapletOriginalCalibration != NULL)
    {
	    delete _ppCTSMMCapletOriginalCalibration;
        _ppCTSMMCapletOriginalCalibration = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::!CCTSMMCapletOriginalCalibration ()
{
    if (_ppCTSMMCapletOriginalCalibration != NULL)
    {
	    delete _ppCTSMMCapletOriginalCalibration;
    }
}
QuantLib::CTSMMCapletOriginalCalibration& Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::GetReference ()
{
    if (_ppCTSMMCapletOriginalCalibration == NULL) throw REFNEW NativeNullException ();
	return **_ppCTSMMCapletOriginalCalibration;
}
boost::shared_ptr<QuantLib::CTSMMCapletOriginalCalibration>& Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::GetShared ()
{
    if (_ppCTSMMCapletOriginalCalibration == NULL) throw REFNEW NativeNullException ();
	return *_ppCTSMMCapletOriginalCalibration;
}
QuantLib::CTSMMCapletOriginalCalibration* Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::GetPointer ()
{
    if (_ppCTSMMCapletOriginalCalibration == NULL) throw REFNEW NativeNullException ();
	return &**_ppCTSMMCapletOriginalCalibration;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CTSMMCapletOriginalCalibration>& Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::GetHandle ()
{
	if (_phCTSMMCapletOriginalCalibration == NULL)
	{
		_phCTSMMCapletOriginalCalibration = new Handle<QuantLib::CTSMMCapletOriginalCalibration> (*_ppCTSMMCapletOriginalCalibration);
	}
	return *_phCTSMMCapletOriginalCalibration;
}
#endif
bool Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::HasNative () 
{
	return (_ppCTSMMCapletOriginalCalibration != NULL);
}

UInt32 Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration::CalibrationFunction (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, Cephei::QL::Models::Marketmodels::IPiecewiseConstantCorrelation^ corr, Cephei::Core::IVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ displacedSwapVariances, Cephei::Core::IVector<Double>^ capletVols, Cephei::QL::Models::Marketmodels::ICurveState^ cs, Double displacement, Cephei::Core::IVector<Double>^ alpha, Boolean lowestRoot, Boolean useFullApprox, UInt64 numberOfFactors, Cephei::Core::IVector<Cephei::QL::Math::IMatrix^>^ swapCovariancePseudoRoots)
{
    CEvolutionDescription^ _Cevolution;
    CPiecewiseConstantCorrelation^ _Ccorr;
    CCurveState^ _Ccs;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Cevolution = safe_cast<CEvolutionDescription^> (evolution);
        _Cevolution->Lock();
        QuantLib::EvolutionDescription& _evolution = static_cast<QuantLib::EvolutionDescription&> (_Cevolution->GetReference ()); 
        _Ccorr = safe_cast<CPiecewiseConstantCorrelation^> (corr);
        _Ccorr->Lock();
        QuantLib::PiecewiseConstantCorrelation& _corr = static_cast<QuantLib::PiecewiseConstantCorrelation&> (_Ccorr->GetReference ()); 
        displacedSwapVariances->Lock();
        INativeVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ _NCIdisplacedSwapVariances = displacedSwapVariances->getFeature (NativeFeature::shared_ptr);
        CPiecewiseConstantVarianceVector^ _NCdisplacedSwapVariances = safe_cast<CPiecewiseConstantVarianceVector^>(_NCIdisplacedSwapVariances);
        std::vector<boost::shared_ptr<
                                QuantLib::PiecewiseConstantVariance> >& _displacedSwapVariances = static_cast<std::vector<boost::shared_ptr<
                                QuantLib::PiecewiseConstantVariance> >&> (_NCdisplacedSwapVariances->GetShared ());
        capletVols->Lock();
        INativeVector<Double>^ _NCIcapletVols = capletVols->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCcapletVols = safe_cast<CDoubleVector^>(_NCIcapletVols);
        std::vector<QuantLib::Volatility>& _capletVols = static_cast<std::vector<QuantLib::Volatility>&> (_NCcapletVols->GetReference ());
        _Ccs = safe_cast<CCurveState^> (cs);
        _Ccs->Lock();
        QuantLib::CurveState& _cs = static_cast<QuantLib::CurveState&> (_Ccs->GetReference ()); 
        QuantLib::Spread _displacement = (QuantLib::Spread)ValueHelper::Convert (displacement); //a
        alpha->Lock();
        INativeVector<Double>^ _NCIalpha = alpha->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCalpha = safe_cast<CDoubleVector^>(_NCIalpha);
        std::vector<QuantLib::Real>& _alpha = static_cast<std::vector<QuantLib::Real>&> (_NCalpha->GetReference ());
        bool _lowestRoot = (bool)ValueHelper::Convert (lowestRoot); //a
        bool _useFullApprox = (bool)ValueHelper::Convert (useFullApprox); //a
        QuantLib::Size _numberOfFactors = (QuantLib::Size)ValueHelper::Convert (numberOfFactors); //a
        swapCovariancePseudoRoots->Lock();
        INativeVector<Cephei::QL::Math::IMatrix^>^ _NCIswapCovariancePseudoRoots = swapCovariancePseudoRoots->getFeature (NativeFeature::Value);
        CMatrixVector^ _NCswapCovariancePseudoRoots = safe_cast<CMatrixVector^>(_NCIswapCovariancePseudoRoots);
        std::vector<QuantLib::Matrix>& _swapCovariancePseudoRoots = static_cast<std::vector<QuantLib::Matrix>&> (_NCswapCovariancePseudoRoots->GetReference ());
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppCTSMMCapletOriginalCalibration)->calibrationFunction ( _evolution,  _corr,  _displacedSwapVariances,  _capletVols,  _cs,  _displacement,  _alpha,  _lowestRoot,  _useFullApprox,  _numberOfFactors,  _swapCovariancePseudoRoots );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cevolution != nullptr) _Cevolution->Unlock();
        if (_Ccorr != nullptr) _Ccorr->Unlock();
        if (displacedSwapVariances != nullptr) displacedSwapVariances->Unlock(); //NO2  
        if (capletVols != nullptr) capletVols->Unlock(); //NO2  
        if (_Ccs != nullptr) _Ccs->Unlock();
        if (alpha != nullptr) alpha->Unlock(); //NO2  
        if (swapCovariancePseudoRoots != nullptr) swapCovariancePseudoRoots->Unlock(); //NO2  
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Models::ICTSMMCapletOriginalCalibration^ Cephei::QL::Models::Marketmodels::Models::CCTSMMCapletOriginalCalibration_Factory::Create (Cephei::QL::Models::Marketmodels::IEvolutionDescription^ evolution, Cephei::QL::Models::Marketmodels::IPiecewiseConstantCorrelation^ corr, Cephei::Core::IVector<Cephei::QL::Models::Marketmodels::Models::IPiecewiseConstantVariance^>^ displacedSwapVariances, Cephei::Core::IVector<Double>^ capletVols, Cephei::QL::Models::Marketmodels::ICurveState^ cs, Double displacement, Cephei::Core::IVector<Double>^ alpha, Boolean lowestRoot, Boolean useFullApprox)
{
    return REFNEW CCTSMMCapletOriginalCalibration ( evolution,  corr,  displacedSwapVariances,  capletVols,  cs,  displacement,  alpha,  lowestRoot,  useFullApprox);
}
